package com.chat.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtils {

    private static String Driver = null;
    private static String Url = null;
    private static String User = null;
    private static String Pass = null;

    static {
        InputStream rs = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {

            properties.load(rs);
            Driver = properties.getProperty("Driver");
            Url = properties.getProperty("url");
            User = properties.getProperty("user");
            Pass = properties.getProperty("pass");

            Class.forName(Driver);

            System.out.println(Driver);
            System.out.println(Url);
            System.out.println(User);
            System.out.println(Pass);

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    //连接
    public static Connection connection(){
        Connection connection = null;

        try{
            connection = DriverManager.getConnection(Url,User,Pass);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

        return connection;

    }

    //断开连接
    public static void closes(Connection cn,PreparedStatement st, ResultSet rs){

        if (rs != null)
        {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (st != null)
        {
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (cn != null)
        {
            try {
                cn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }


}
